<template>
  <div>
    <el-upload
      action="#"
      list-type="picture-card"
      :auto-upload="false"
      :limit="1"
      :file-list="imgFileList"
      accept="image/gif,image/jpeg,image/jp2"
    >
      <i slot="default" class="el-icon-plus" />
      <div slot="file" slot-scope="{file}">
        <img
          class="el-upload-list__item-thumbnail"
          :src="file.url"
          :alt="handleAvatarFilee(file)"
        >
        <span class="el-upload-list__item-actions">
          <span
            class="el-upload-list__item-preview"
            @click="handlePictureCardPreview(file)"
          >
            <i class="el-icon-zoom-in" />
          </span>
          <span
            class="el-upload-list__item-delete"
            @click="handleRemove(file)"
          >
            <i class="el-icon-delete" />
          </span>
        </span>
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    <el-form ref="dataForm" :rules="rules" :model="temp">
      <el-form-item label="姓名">
        <el-input v-model.trim="temp.userName" maxlength="128" />
      </el-form-item>
      <el-form-item label="联系电话">
        <el-input v-model.trim="temp.userPhone" maxlength="32" />
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model.trim="temp.userEmail" maxlength="128" />
      </el-form-item>
      <el-form-item>
        <el-button :loading="ajaxLoading" type="primary" @click="submit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { updateUserInfo, getUserInfo } from '@/api/user/userProfile'
import { getImageUrl } from '@/utils/image'

export default {
  name: 'Account',
  props: {
    user: {
      type: Object,
      default: () => {
        return {
          name: '',
          userInfo: {}
        }
      }
    }
  },
  data() {
    return {
      ajaxLoading: false,
      dialogImageUrl: '',
      dialogVisible: false,
      imgFileList: [],
      avatarFile: null,
      temp: {
        userName: '',
        userAvatar: '',
        userPhone: '',
        userEmail: ''
      },
      rules: {
        userName: [{ max: 128, message: '姓名长度不能大于128个字符', trigger: 'change' }],
        userPhone: [{ max: 32, message: '联系电话长度不能大于32个字符', trigger: 'change' }],
        userEmail: [{ max: 128, message: '邮箱长度不能大于128个字符', trigger: 'change' }]
      }
    }
  },
  created() {
    this.loadUserInfo()
  },
  methods: {
    handleAvatarFilee(file) {
      this.avatarFile = file
      return ''
    },
    handleRemove(file) {
      this.imgFileList = []
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    getImageUrl1(uri) {
      return getImageUrl(uri)
    },
    loadUserInfo() {
      getUserInfo().then((response) => {
        this.temp = Object.assign({}, response.data)
        this.imgFileList = [{ name: '', url: getImageUrl(response.data.userAvatar) }]
        this.$store.state.user.userInfo.userName = response.data.userName
        this.$store.state.user.userInfo.userPhone = response.data.userPhone
        this.$store.state.user.userInfo.userEmail = response.data.userEmail
        this.$store.state.user.userInfo.userAvatar = response.data.userAvatar
      })
    },
    submit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          const fileObj = this.avatarFile && this.avatarFile.raw ? this.avatarFile.raw : null
          this.ajaxLoading = true
          updateUserInfo(fileObj, tempData).then((response) => {
            this.loadUserInfo()
            this.ajaxLoading = false
            this.$message({
              message: '提交处理成功！',
              type: 'success'
            })
            // 返回值处理
            this.loadUserInfo()
          }, (response) => {
            this.ajaxLoading = false
          })
        }
      })
    }
  }
}
</script>
